package com.tide.crm.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.tide.crm.dao.ConvergeRoleDao;
import com.tide.crm.dto.ConvergeRoleDto;
import com.tide.crm.model.ConvergeRole;
import com.tide.crm.service.ConvergeRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tide.crm.page.table.PageTableHandler;
import com.tide.crm.page.table.PageTableHandler.CountHandler;
import com.tide.crm.page.table.PageTableHandler.ListHandler;
import com.tide.crm.page.table.PageTableRequest;
import com.tide.crm.page.table.PageTableResponse;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/convergeRoles")
public class ConvergeRoleController {

	@Resource(name="convergeRoleService")
	private ConvergeRoleService convergeRoleService;
	
    @Autowired
    private ConvergeRoleDao convergeRoleDao;

    @PostMapping
    @ApiOperation(value = "保存")
    public void save(@RequestBody ConvergeRoleDto convergeRole, HttpServletResponse response) throws IOException {
        boolean flag = convergeRoleService.saveConvergeRole(convergeRole);
        if(flag) {
        	response.getWriter().write(1);
        }else {
        	response.getWriter().write(2);
        }
        
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取")
    public ConvergeRole get(@PathVariable Long id) {
        return convergeRoleService.getConvergeRoleById(id);
    }

    @PutMapping
    @ApiOperation(value = "修改")
    public ConvergeRole update(@RequestBody ConvergeRole convergeRole) {
    	
    	convergeRoleService.updateConvergeRole(convergeRole);

        return convergeRole;
    }

    @GetMapping
    @ApiOperation(value = "列表")
    public PageTableResponse list(PageTableRequest request) {
        return new PageTableHandler(new CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                return convergeRoleDao.count(request.getParams());
            }
        }, new ListHandler() {

            @Override
            public List<ConvergeRole> list(PageTableRequest request) {
                return convergeRoleDao.list(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public void delete(@PathVariable Long id) {
        convergeRoleDao.delete(id);
    }
}
